package cn.tedu.egweibo5.controller;

import cn.tedu.egweibo5.common.JsonResult;
import cn.tedu.egweibo5.common.StatusCode;
import cn.tedu.egweibo5.mapper.CommentMapper;
import cn.tedu.egweibo5.pojo.dto.CommentDTO;
import cn.tedu.egweibo5.pojo.entity.Comment;
import cn.tedu.egweibo5.pojo.entity.User;
import cn.tedu.egweibo5.pojo.entity.Weibo;
import cn.tedu.egweibo5.pojo.vo.UserVO;
import cn.tedu.egweibo5.pojo.vo.WeiboDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/comments/")
@Api(tags = "03.评论管理模块")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation("发布评论")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");

        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation("查询微博评论")
    public JsonResult selectCommentByWeiboId(Integer id) {
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }

}
